package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.wonders.frame.common.Enum.ResponseCodeEnum;
import com.wonders.frame.common.Enum.SessionKeyEnum;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthAccount;
import com.wonders.frame.entity.AuthUser;
import com.wonders.frame.entity.form.AuthUserForm;
import com.wonders.frame.service.AuthUserService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/authUser")
public class AuthUserController extends CoreController<AuthUser> {
    @Autowired
    private AuthUserService authUserService;
    @Override
    protected CoreCurdService getService() {
        return authUserService;
    }

    private final ReentrantLock lock=new ReentrantLock();

    @PostMapping("/save")
    public Response save(HttpServletRequest request,@RequestBody AuthUserForm authUserForm){
        try {
            lock.lock();
            //判断唯一性
            if(!authUserService.checkUniqueAccount(authUserForm)){
                return errorResponse(ResponseCodeEnum.ACCOUNT_REPEAT,null);
            }
            if(!authUserService.checkUniqueCertnum(authUserForm)){
                return errorResponse(ResponseCodeEnum.CERTNUM_REPEAT,null);
            }
            HttpSession session = request.getSession();
            AuthAccount acc = (AuthAccount)session.getAttribute(SessionKeyEnum.USERINFO.name());

            AuthUser authUser=authUserService.save(authUserForm,acc);
            return successResponse(authUser);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse(ResponseCodeEnum.SYSTEM_ERROR,null);
        }finally {
            lock.unlock();
        }

    }

    @PostMapping("/deleteById")
    public Response deleteById(String id){
        try {
            authUserService.deleteById(id);
            return successResponse(null);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }

    //转出
    @PostMapping("/changeStatus")
    public Response changeStatus(HttpServletRequest request,String type,String userId,String account){
        try {
            //验证是否是自己的账号 是自己的账号不让转入转出
            if(authUserService.isSelf(userId,account)){
                return errorResponse(ResponseCodeEnum.SELF_ACCOUNT,null);
            }

            if("out".equals(type)){
                authUserService.changeStatusOut(userId);
            }else if("in".equals(type)){
                HttpSession session = request.getSession();
                AuthAccount acc = (AuthAccount)session.getAttribute(SessionKeyEnum.USERINFO.name());
                authUserService.changeStatusIn(acc.getOrgaId(),acc.getDeptId(),userId);
            }
            return successResponse(null);
        }catch (Exception e){
            e.printStackTrace();
            return errorResponse();
        }
    }



}
